/**
 * 
 */
package wpmp.security.mgr.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Component;

import wpmp.security.mgr.base.BaseDao;
import wpmp.security.mgr.entity.RoleResourceRela;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 11:35:58 PM Mar 17, 2013
 */
@Component
public class RoleResourceRelaDao extends BaseDao<RoleResourceRela, Long> {

	private static final String SQL_DELETE_ROLE_RESOURCE_OPERATION_RELA = "DELETE FROM sys_role_resource_operation_rela WHERE rela_id=?";

	/**
	 * 删除角色、资源、操作类型之间的关系
	 * 
	 * @param relaId
	 * @return
	 */
	public Integer deleteOperationRela(final Long relaId) {
		return getJdbcTemplate().execute(SQL_DELETE_ROLE_RESOURCE_OPERATION_RELA,
				new PreparedStatementCallback<Integer>() {
					@Override
					public Integer doInPreparedStatement(PreparedStatement ps) throws SQLException,
							DataAccessException {
						ps.setLong(1, relaId);
						return ps.executeUpdate();
					}
				});
	}

	/**
	 * 删除角色、资源、操作类型之间的关系
	 * 
	 * @param relaIds
	 * @return
	 */
	public int[] deleteOperationRela(final Long[] relaIds) {
		return getJdbcTemplate().batchUpdate(SQL_DELETE_ROLE_RESOURCE_OPERATION_RELA,
				new BatchPreparedStatementSetter() {

					@Override
					public void setValues(PreparedStatement ps, int i) throws SQLException {
						ps.setLong(1, relaIds[i]);
					}

					@Override
					public int getBatchSize() {
						return relaIds.length;
					}
				});
	}
}
